<template>
  <div class="employees-container">
    <div class="app-container">
      <page-tools>
        <template #left>
          <span>总记录数: {{ total }} 条</span>
        </template>
        <template #right>
          <el-button type="warning" size="small">excel导入</el-button>
          <el-button type="danger" size="small">excel导出</el-button>
          <el-button type="primary" size="small" @click="dialogVisible=true">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 10px">
        <el-table border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{row}">
              {{ formOfEmploymentFormat(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="correctionTime" sortable />
          <!-- <el-table-column label="账户状态" prop="" /> -->
          <el-table-column label="操作" width="280">
            <template>
              <el-button type="text" size="small">查看</el-button>
              <el-button type="text" size="small">分配角色</el-button>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row
          type="flex"
          justify="center"
          align="middle"
          style="height: 60px"
        >
          <el-pagination
            :current-page="pageForm.page"
            :page-sizes="[3, 5, 10, 20, 99]"
            :page-size="pageForm.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-row>
      </el-card>

      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
      >
        <empDialog />
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getUserList } from '@/api/empioyees'
import employees from '@/constant/employees'
import empDialog from '@/views/employees/empDialog.vue'
// 取出枚举文件中的对象
const hireType = {}
employees.hireType.forEach((item) => {
  hireType[item.id] = item.value
})

export default {
  name: 'Employees',
  components: {
    empDialog
  },
  data() {
    return {
      // 控制弹层开关
      dialogVisible: false,
      // 存储表格数据
      list: [],
      // 分页
      pageForm: {
        page: 1, // 当前页码
        size: 10 // 每页显示的条数
      },
      total: 0 // 数据总和
    }
  },
  created() {
    this.hGet()
  },
  methods: {
    // 获取员工列表信息
    async hGet() {
      try {
        // 发送请求获取数据
        const res = await getUserList(this.pageForm)
        console.log(res, '获取员工列表')
        // 存储数据
        this.list = res.data.rows
        this.total = res.data.total
      } catch (error) {
        console.log(error)
      }
    },
    // 使用枚举文件转化数据,利用作用域插槽传入数据进行转换
    formOfEmploymentFormat(data) {
      return hireType[data] || '未知'
    },

    // 分页器事件
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
      // 回到第一页
      this.pageForm.page
      // 重新修改当前页面条数
      this.pageForm.size = val
      // 重新渲染页面
      this.hGet()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
      // 重新修改当前页
      this.pageForm.page = val
      // 重新渲染数据
      this.hGet()
    }
  }
}
</script>
